package com.shjn.squirrel.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.shjn.squirrel.R;

/**
 * 垃圾去向对话框
 */
public class WasteDirectionDialog extends BaseDialogFragment implements View.OnClickListener {


    private static WasteDirectionDialog instance;
    private Thread mThread;

    public WasteDirectionDialog() {
    }

    public synchronized static WasteDirectionDialog getInstance() {
        if (instance == null) {
            instance = new WasteDirectionDialog();
        }
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //设置对话框资源文件
        setLayoutResource(R.layout.user_waste_direction_dialog);
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void onResume() {
        //设置对话框大小
        setdialogSize(300, 347, -35);
        //初始化控件
        ((Button) mDialog.findViewById(R.id.btn_ok)).setOnClickListener(this);
        //默认30秒自动关闭对话框
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(30000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mDialog.cancel();
                        mDialog.dismiss();
                        if (mOnCloseDialogListener != null) {
                            mOnCloseDialogListener.onCloseDialog();
                        }
                    }
                });
            }
        });
        mThread.start();
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //确定
            case R.id.btn_ok:
                closeDialog();
                break;
            default:
                break;
        }
    }

    /**
     * 关闭对话框
     */
    public void closeDialog() {
        if (mThread != null) {
            mThread.interrupt();
        }
    }
}
